package database;
import java.io.*;

public class StorageManager {
	String dbName;

	StorageManager(String dbName){
		this.dbName= dbName;
		// Create a directory which name is the name of the database.
		File file = new File("./"+ dbName); 
		file.mkdir();
	}
	
	
	public void saveSchema(Table t, Object[][] tableSchema){
		try{
			String url = this.dbName + "/" + t.name + ".frm";
			Object[][] schema = t.structure;
        	DataOutputStream file = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(new File(url))));
        	int i;
        	for(i=0; i < schema.length; i++){
        		file.writeChars(schema[i][0].toString() + "," + schema[i][1].toString() + "," + schema[i][2].toString());
				file.writeChars("\n");
        	}
        	file.close();
        	// we create an empty MYD file for purpose of initialization
        	url = this.dbName + "/" + t.name + "_0.MYD";
        	DataOutputStream file2 = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(new File(url))));
        	file2.close();
		}
		catch (java.io.IOException e){
			
		}
		
	}
	
	
	
	public boolean store(Page page){			//store a page in the hard drive and returns true if it was successful and false otherwise.
		String tableName = page.tableName;
		int numberOfRecord = page.numberOfRecord;
		int number = page.number;

   		try {
   			String url = this.dbName + "/" + tableName + "_" + number + ".MYD";
        	DataOutputStream file = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(new File(url))));
        	int i = 0;
        	while (i < numberOfRecord) {
        		Record storedRecord = page.content[i];
        		RecordID rid = storedRecord.rid; 
        		Object[][] data = storedRecord.data;
        		int j;
        		file.writeChars(rid.rid + ",");
        		for (j=0; j < data.length ; j++){																		
        			file.writeChars(data[j][1].toString());			
    				file.writeChars(",");
        			
        			/*if( (page.structure[j][2]).toString().equals("int") ){ // this element is an INT
        				file.writeInt(Integer.parseInt(data[j].toString()));
        				file.writeChars(",");
        			}
        			else if ( (page.structure[j][2]).equals("varchar") ){ // this element is a VARCHAR
        				file.writeChars(data[j].toString());
        				file.writeChars(",");
        			}*/
        		}
        		file.writeChars("\n");
        		i++;
        	}
 			file.close();
    	}
   		catch (java.io.IOException e) {
   			e.printStackTrace();
   		}
   		
   		return true;
	}
	
	public Page getLastPageOfTable(Table table) throws IOException{		// returns the last page of the table 'table' from the hard drive				
		return this.getPageOfTable(table, table.numberPages);
	}
	
	public Page getPageOfTable(Table table, int pageNumber){
		Page returnPage = new Page(table,pageNumber);

		try{
			// First get the structure of the table														
			Object[][] structure;
			Object[][] temp = new Object[20][3];
			BufferedReader structFile = new BufferedReader(new FileReader((this.dbName + "/" + table.name + ".frm")));
			String line;
			int numberOfFields = 0;
			while ((line = structFile.readLine()) != null){
				temp[numberOfFields][0] = line.split(",")[0];
				temp[numberOfFields][1] = line.split(",")[1];
				temp[numberOfFields][2] = line.split(",")[2];
				numberOfFields++;
				}
			
			structure = new Object[numberOfFields][3];
			int i=0;
			for(i=0 ; i < numberOfFields ; i++){
				structure[i][0] = temp[i][0];
				structure[i][1] = temp[i][1];
				structure[i][2] = temp[i][2];
			}
			
			// Then get the record of the table page
			BufferedReader file = new BufferedReader(new FileReader((this.dbName + "/" + table.name + "_" + pageNumber + ".MYD")));     	
			int numberOfRecord = 0;
			while ((line = file.readLine()) != null){
				Record lineRecord = new Record(line,structure);		// lineRecord is filled with data from the line
				returnPage.content[numberOfRecord] = lineRecord;
				numberOfRecord++;
     			}

			file.close();
		}
    	catch(IOException e){
			System.out.println("This page doesn't exists for this table");
     	}
    	return returnPage;
	}

}